package org.srishti.mobile;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;

import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class LoginActivity extends Activity {
	
	
	public final static String EXTRA_MESSAGE = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
              
		SharedPreferences sp =getPreferences(MODE_PRIVATE);
		if (sp.contains("username") && sp.contains("password"))	{
		String uname=sp.getString("username","");
		String pword=sp.getString("password","");
		EditText loginEbx = (EditText)findViewById(R.id.u_sername);
        EditText passEbx = (EditText)findViewById(R.id.p_assword);
        loginEbx.setText(uname);
        passEbx.setText(pword);
		}

    }

    public void onClick(View v) {
        // Switching to Register screen
    	    	
    	EditText editText=(EditText) findViewById(R.id.u_sername);
    	String username=editText.getText().toString();
    	EditText editText1=(EditText) findViewById(R.id.p_assword);
    	String password=editText1.getText().toString();
    	Intent intent = new Intent(getApplicationContext(), PreparationActivity.class);
    	
    		switch (v.getId()) {
    	    case R.id.btnLogin:
    		 
    	    	SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
    	  	    editor.putString("username", username);
    	  	    editor.putString("password", password);
    	  	    // Commit the edits!
    	  	    editor.commit();
    	      intent.putExtra(EXTRA_MESSAGE,username+" Login Succefull");
    	      startActivity(intent);
    	
    	
    	break;
    	case R.id.btnCancel:
    		ViewGroup group = (ViewGroup)findViewById(R.id.clear_group);
    		for (int i = 0, count = group.getChildCount(); i < count; ++i) {
    		    View view = group.getChildAt(i);
    		    if (view instanceof EditText) {
    		        ((EditText)view).setText("");
    		    }
    		}
  	      break;
    	
    		}
       
    }

}
